package com.app.studyroom;

import android.app.Application;
import android.util.Log;

import com.app.studyroom.api.ApiClient;
import com.app.studyroom.api.ApiService;
import com.app.studyroom.api.model.ssrGet.ClassroomResponse;
import com.app.studyroom.db.AreaInfo;
import com.app.studyroom.api.ApiResponse;
import com.app.studyroom.mapper.AnnouncementTypeMapper;
import com.app.studyroom.mapper.ApprovalStatusMapper;
import com.app.studyroom.mapper.AreaNameToIdMapper;
import com.app.studyroom.mapper.ClassroomInfoMapper;
import com.app.studyroom.mapper.MessageReadStatusMapper;
import com.app.studyroom.mapper.SeatMapper;
import com.app.studyroom.mapper.SeatStatusMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        initAreaMapper(); ///???
        initSeatMapper();
        initSeatStatusMapper();
        initApprovalStatusMapper();
        initClassroomMapping();
        initAnnouncementTypeMapper();
    }

    private void initAreaMapper() {
        ApiService apiService = ApiClient.getClient().create(ApiService.class);
        apiService.getAreaList().enqueue(new Callback<ApiResponse<List<AreaInfo>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<AreaInfo>>> call, Response<ApiResponse<List<AreaInfo>>> response) {
                if (response.body() != null && response.body().getCode() == 200) {
                    List<AreaInfo> areaList = response.body().getData();
                    AreaNameToIdMapper.updateMappingFromApi(areaList);  // ✅ 初始化 Area 映射
                    Log.d("MyApplication", "区域与座位映射初始化完成，共加载：" + areaList.size());
                } else {
                    Log.e("MyApplication", "接口返回失败：" + response.code());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<AreaInfo>>> call, Throwable t) {
                Log.e("MyApplication", "区域映射初始化失败，保留默认映射", t);
            }
        });
    }
    private void initSeatStatusMapper() {
        ApiService apiService = ApiClient.getClient().create(ApiService.class);
        apiService.getSeatStatusEnum().enqueue(new Callback<ApiResponse<Map<String, String>>>() {
            @Override
            public void onResponse(Call<ApiResponse<Map<String, String>>> call, Response<ApiResponse<Map<String, String>>> response) {
                if (response.isSuccessful() && response.body() != null && response.body().getCode() == 200) {
                    SeatStatusMapper.updateMappingFromApi(response.body().getData());
                    Log.d("MyApplication", "座位状态枚举初始化完成：" + response.body().getData().size());
                } else {
                    Log.e("MyApplication", "获取座位状态枚举失败，code=" + response.code());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<Map<String, String>>> call, Throwable t) {
                Log.e("MyApplication", "请求座位状态枚举失败", t);
            }
        });
    }
    private void initApprovalStatusMapper() {
        ApiService apiService = ApiClient.getClient().create(ApiService.class);
        apiService.getApprovalStatusEnum().enqueue(new Callback<ApiResponse<Map<String, String>>>() {
            @Override
            public void onResponse(Call<ApiResponse<Map<String, String>>> call, Response<ApiResponse<Map<String, String>>> response) {
                if (response.isSuccessful() && response.body() != null && response.body().getCode() == 200) {
                    ApprovalStatusMapper.updateMappingFromApi(response.body().getData());
                    Log.d("MyApplication", "审批状态映射初始化完成：" + response.body().getData().size());
                } else {
                    Log.e("MyApplication", "审批状态接口失败，code=" + response.code());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<Map<String, String>>> call, Throwable t) {
                Log.e("MyApplication", "审批状态请求失败", t);
            }
        });
    }
    private void initClassroomMapping() {
        ApiService apiService = ApiClient.getClient().create(ApiService.class);
        Call<ApiResponse<List<ClassroomResponse>>> call = apiService.getClassroomTree();

        call.enqueue(new Callback<ApiResponse<List<ClassroomResponse>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<ClassroomResponse>>> call, Response<ApiResponse<List<ClassroomResponse>>> response) {
                if (response.isSuccessful() && response.body() != null && response.body().getData() != null) {
                    ClassroomInfoMapper.clear();

                    for (ClassroomResponse parent : response.body().getData()) {
                        if ("1".equals(parent.cidNum)) {
                            ClassroomInfoMapper.put(parent.id, parent.qyName, parent.qyDz);
                            ClassroomInfoMapper.addAreaId(parent.id);

                            if (parent.children != null) {
                                for (ClassroomResponse child : parent.children) {
                                    if ("2".equals(child.cidNum)) {
                                        ClassroomInfoMapper.put(child.id, child.qyName, child.qyDz);
                                        ClassroomInfoMapper.addClassroomId(child.id, parent.id);
                                    }
                                }
                            }
                        }
                    }

                    // 日志测试
                    Log.d("ClassroomMapperTest", "区域ID列表：" + ClassroomInfoMapper.getAreaIdList());
                    Log.d("ClassroomMapperTest", "教室ID列表：" + ClassroomInfoMapper.getClassroomIdList());
                    Log.d("ClassroomMapperTest", "500教室名称：" + ClassroomInfoMapper.getName("500"));
                    Log.d("ClassroomMapperTest", "501教室地址：" + ClassroomInfoMapper.getAddress("501"));
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<ClassroomResponse>>> call, Throwable t) {
                Log.e("ClassroomMapperTest", "请求失败：" + t.getMessage());
            }
        });
    }
    private void initSeatMapper() {
        ApiService apiService = ApiClient.getClient().create(ApiService.class);
        apiService.getClassroomTree().enqueue(new Callback<ApiResponse<List<ClassroomResponse>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<ClassroomResponse>>> call, Response<ApiResponse<List<ClassroomResponse>>> response) {
                if (response.isSuccessful() && response.body() != null && response.body().getData() != null) {
                    List<ClassroomResponse> tree = response.body().getData();

                    // ✅ 用新的接口数据初始化 seat 映射
                    SeatMapper.updateRoomMappingFromClassroomTree(tree);

                    Log.d("SeatMapperInit", "seat mapper 初始化完成: " + SeatMapper.getRoomIdMapAsString());
                } else {
                    Log.e("SeatMapperInit", "seat mapper 初始化失败，code = " + response.code());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<ClassroomResponse>>> call, Throwable t) {
                Log.e("SeatMapperInit", "seat mapper 请求失败", t);
            }
        });
    }
    private void initAnnouncementTypeMapper() {
        ApiService apiService = ApiClient.getClient().create(ApiService.class);
        apiService.getMessagesEnum().enqueue(new Callback<ApiResponse<Map<String, String>>>() {
            @Override
            public void onResponse(Call<ApiResponse<Map<String, String>>> call, Response<ApiResponse<Map<String, String>>> response) {
                if (response.isSuccessful() && response.body() != null && response.body().getCode() == 200) {
                    AnnouncementTypeMapper.initFromMap(response.body().getData());
                    Log.d("MyApplication", "公告类型映射初始化成功：" + response.body().getData().size());
                } else {
                    Log.e("MyApplication", "公告类型接口失败，code=" + response.code());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<Map<String, String>>> call, Throwable t) {
                Log.e("MyApplication", "公告类型请求失败", t);
            }
        });
    }

    //预留读取状态枚举接口
    private void initMessageReadStatusMapper() {
//        ApiService apiService = ApiClient.getClient().create(ApiService.class);
//
//        // 此接口未来若上线，可直接启用
//        apiService.getMessageReadStatusEnum().enqueue(new Callback<ApiResponse<Map<String, String>>>() {
//            @Override
//            public void onResponse(Call<ApiResponse<Map<String, String>>> call, Response<ApiResponse<Map<String, String>>> response) {
//                if (response.isSuccessful() && response.body() != null && response.body().getCode() == 200) {
//                    MessageReadStatusMapper.initFromMap(response.body().getData());
//                    Log.d("MyApplication", "消息状态枚举已从接口更新");
//                }
//            }
//
//            @Override
//            public void onFailure(Call<ApiResponse<Map<String, String>>> call, Throwable t) {
//                Log.w("MyApplication", "接口未启用或请求失败，使用默认消息状态映射");
//            }
//        });
    }
}
